package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.auth.model.LoginUser;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.BtyZxFeiyongfentan;
import cn.stylefeng.guns.sys.modular.system.entity.ZxChengbenMingxi;
import cn.stylefeng.guns.sys.modular.system.mapper.*;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyZxFeiyongfentanParam;
import cn.stylefeng.guns.sys.modular.system.model.params.ZxChengbenMingxiParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyZxFeiyongfentanResult;
import cn.stylefeng.guns.sys.modular.system.service.BtyZxFeiyongfentanService;
import cn.stylefeng.guns.sys.modular.system.service.ZxChengbenMingxiService;
import cn.stylefeng.roses.core.reqres.response.ErrorResponseData;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2021-08-30
 */
@Service
public class BtyZxFeiyongfentanServiceImpl extends ServiceImpl<BtyZxFeiyongfentanMapper, BtyZxFeiyongfentan> implements BtyZxFeiyongfentanService {

    @Resource
    private BtyBrandMapper btyBrandMapper;
    @Resource
    private ZxChengbenMingxiService zxChengbenMingxiService;
    @Resource
    private MdzxMapper mdzxMapper;
    @Resource
    private BtyBxdaMapper btyBxdaMapper;
    @Resource
    private ZxChengbenMingxiMapper zxChengbenMingxiMapper;

    @Override
    public void add(BtyZxFeiyongfentanParam param){
        param.setStatus("2");//状态： 1->已生成, 2->未生成
        param.setCreateTime(new Date());
        param.setCreateUser(LoginContextHolder.getContext().getUserId());
        if (param.getType().equals("0")){//店面房租
            param.setKemuId(1325729688980951041L);
            param.setKeywordId(1099);
        }else if (param.getType().equals("1")){//工资
            param.setKemuId(1325729804877959170L);
            param.setKeywordId(1240);
        }else if (param.getType().equals("2")){//配送进货
            param.setKemuId(1325730007240544257L);
            param.setKeywordId(1308);
        }
        BtyZxFeiyongfentan entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BtyZxFeiyongfentanParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BtyZxFeiyongfentanParam param){
        param.setUpdateTime(new Date());
        param.setUpdateUser(LoginContextHolder.getContext().getUserId());
        BtyZxFeiyongfentan oldEntity = getOldEntity(param);
        BtyZxFeiyongfentan newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public BtyZxFeiyongfentanResult findBySpec(BtyZxFeiyongfentanParam param){
        return null;
    }

    @Override
    public List<BtyZxFeiyongfentanResult> findListBySpec(BtyZxFeiyongfentanParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(BtyZxFeiyongfentanParam param){
        Page pageContext = getPageContext();

        //当前登录者姓名就是会计的名字
        LoginUser currentUser = LoginContextHolder.getContext().getUser();
        String userName = currentUser.getName();
        if (userName.equals("admin")){
            userName = "";
        }

        IPage page = this.baseMapper.customPageList(pageContext, param, userName);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<Map> dmList() {

        //当前登录者姓名就是会计的名字
        LoginUser currentUser = LoginContextHolder.getContext().getUser();
        String userName = currentUser.getName();
        if (userName.equals("admin")){
            userName = "";
        }
        List<Map> list = btyBrandMapper.dmFentan(userName);
        return list;
    }

    private Serializable getKey(BtyZxFeiyongfentanParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BtyZxFeiyongfentan getOldEntity(BtyZxFeiyongfentanParam param) {
        return this.getById(getKey(param));
    }

    private BtyZxFeiyongfentan getEntity(BtyZxFeiyongfentanParam param) {
        BtyZxFeiyongfentan entity = new BtyZxFeiyongfentan();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    /**
     * 生成报销单
     * @param param
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData addBxd(BtyZxFeiyongfentanParam param) {

        //获取btyZxMdzxId
        String btyZxMdzxId = mdzxMapper.getMdzxId(param.getYaoma());
        if (ToolUtil.isEmpty(btyZxMdzxId)){
            return ResponseData.error("门店装修中没有此数据，不能保存！");
        }
        //获取报销人
        Map bxr = btyBxdaMapper.getBxr(param.getKh());
        if (ToolUtil.isEmpty(bxr)){
            return ResponseData.error("没有报销人，不能保存！");
        }

        ZxChengbenMingxiParam mingxiParam = new ZxChengbenMingxiParam();
        mingxiParam.setBtyyBxdaIdView(bxr.get("xm").toString());
        mingxiParam.setBtyyBxdaId(bxr.get("id").toString());
        mingxiParam.setBtyZxMdzxIds(btyZxMdzxId);
        mingxiParam.setKeywordId(param.getKeywordId());
//        mingxiParam.setShuliang();
//        mingxiParam.setUnitPrice();
        mingxiParam.setJine(param.getCost());
        mingxiParam.setStatus("6");//预留
        mingxiParam.setSrcType("bty_zx_feiyongfentan");
        mingxiParam.setSrcValue(param.getId().toString());
        zxChengbenMingxiService.addMany(mingxiParam);
        param.setStatus("1");

        BtyZxFeiyongfentan oldEntity = getOldEntity(param);
        BtyZxFeiyongfentan newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        baseMapper.updateById(newEntity);
        return ResponseData.success();
    }

    /**
     * 撤回报销单
     * @param param
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData rebutBxd(BtyZxFeiyongfentanParam param) {

        //查询报销单状态 status = 6才可删除
        ZxChengbenMingxiParam mingxiParam = new ZxChengbenMingxiParam();
        mingxiParam.setSrcType("bty_zx_feiyongfentan");
        mingxiParam.setSrcValue(param.getId().toString());
        ZxChengbenMingxi mingxi = zxChengbenMingxiMapper.getId(mingxiParam);

        if (mingxi.getStatus().equals("6")){
            ZxChengbenMingxiParam param1 = new ZxChengbenMingxiParam();
            param1.setBtyZxChengbenMingxiId(mingxi.getBtyZxChengbenMingxiId());
            boolean isDel = zxChengbenMingxiService.deleteBxd(param1);

            if(isDel == false){
                return new ErrorResponseData("撤回报销单失败");
            }
            //费用分摊表中修改数据状态
            param.setStatus("2");

            BtyZxFeiyongfentan oldEntity = getOldEntity(param);
            BtyZxFeiyongfentan newEntity = getEntity(param);
            ToolUtil.copyProperties(newEntity, oldEntity);
            baseMapper.updateById(newEntity);
        }else {
            String sta = "";

            if (mingxi.getStatus().equals("5")){
                sta = "被打回";
            }else if (mingxi.getStatus().equals("3")){
                sta = "待审核";
            }else if (mingxi.getStatus().equals("2")){
                sta = "待打款";
            }else if (mingxi.getStatus().equals("1")){
                sta = "已打款";
            }else if (mingxi.getStatus().equals("9")){
                sta = "负单";
            }else if (mingxi.getStatus().equals("10")){
                sta = "已删除";
            }
            return ResponseData.error("该报销单已处于" + sta + "状态，不可撤回！");
        }

        return ResponseData.success();
    }

    @Override
    public LayuiPageInfo listBxd(BtyZxFeiyongfentanParam param) {
        Page pageContext = getPageContext();

        IPage page = this.baseMapper.customPageList(pageContext, param, null);
        return LayuiPageFactory.createPageInfo(page);
    }

}
